﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace CIRDiagnoseDevice
{
    public partial class UserManager : Form
    {
        int SelectedNum;
        public UserManager()
        {
            
            InitializeComponent();
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = LoadDataBase();
            //textBox1.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
            dataGridView1.AutoGenerateColumns = false;
            //textBox2.Text = dataGridView1.Rows[0].Cells[2].Value.ToString();
        }

        public OleDbConnection InitialConnectionDb()
        {
            string constr = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source =" + Application.StartupPath + @"\" + "UserInfo.accdb" + "; Jet OLEDB:Database Password = 430510;";
                OleDbConnection conn = new OleDbConnection(constr);
                return conn;
        }
       public DataTable LoadDataBase()
       {
                OleDbDataAdapter oldat = new OleDbDataAdapter();
                OleDbConnection conn = InitialConnectionDb();
                DataTable datb = new DataTable();
                string querylan = "Select * From UserInfo";
                OleDbCommand command = new OleDbCommand(querylan, conn);
                command.CommandType = CommandType.Text;
           try
           {
               conn.Open();
               oldat.SelectCommand = command;
               oldat.Fill(datb);
           }
           catch (System.Exception ex)
           {
              //MessageBox.Show(ex.Message);
           }
           conn.Close();
           return datb;
       }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                OleDbConnection conn = InitialConnectionDb();
                string querylan = "Select 用户名 From UserInfo Where 用户名='" +textBox1.Text.Trim() +"'";
                   OleDbCommand command = new OleDbCommand(querylan, conn);
                command.CommandType = CommandType.Text;
           try
           {
               conn.Open();
               OleDbDataReader read = command.ExecuteReader();
               if (read.Read())
               {
                   MessageBox.Show("此用户名已存在，请重新输入用户名");
                   read.Close();
                   conn.Close();
               }
               else
               {
                   string querylan2 = "Insert Into UserInfo (用户名,密码) VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
                   OleDbCommand command2 = new OleDbCommand(querylan2, conn);
                   int num = command2.ExecuteNonQuery();
                   if (num == 1)
                   {
                       MessageBox.Show("用户添加成功");
                       conn.Close();
                       dataGridView1.DataSource = LoadDataBase();
                       conn.Close();
                   }
                   else
                   {
                       MessageBox.Show("用户添加失败");
                       conn.Close();
                   }
               }
               conn.Close();
           }
           catch (System.Exception ex)
           {
               MessageBox.Show(ex.Message);
               conn.Close();
           }
         
            }
            else
            {
                MessageBox.Show("用户名和密码i不能为空");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string selectstr = dataGridView1.Rows[SelectedNum].Cells[0].Value.ToString();
            if (selectstr != "" && selectstr != "Admin")
            {
                if ((MessageBox.Show("确定删除该用户？", "删除用户", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                {
                    OleDbConnection conn = InitialConnectionDb();
                    string querylan = "Delete From UserInfo Where 用户名='" + selectstr + "'";
                    OleDbCommand command = new OleDbCommand(querylan, conn);
                    command.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        command.ExecuteNonQuery();
                        conn.Close();
                        dataGridView1.DataSource = LoadDataBase();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    conn.Close();
                }
            }
            else
            {
                MessageBox.Show("不能删除管理员!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = InitialConnectionDb();
            string querylan = "Select 用户名 ,密码 From UserInfo Where 用户名='" + textBox3.Text.Trim() + "'" +"And 密码='"+textBox4.Text.Trim() +"'";
            OleDbCommand command = new OleDbCommand(querylan, conn);
            command.CommandType = CommandType.Text;
            try
            {
                if (textBox3.Text == "")
                {
                    MessageBox.Show("请选择一个用户！ ");
                }
                else if (textBox4.Text == "")
                {
                    MessageBox.Show("请输入原始密码！");
                }
                else
                {
                    conn.Open();
                    OleDbDataReader reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        if (textBox5.Text != "")
                        {
                            if (textBox5.Text.Trim() == textBox6.Text.Trim())
                            {
                                string querylan2 = "Update UserInfo Set 密码='" + textBox6.Text.Trim() + "'Where 用户名= '" + textBox3.Text.Trim() + "'";
                                OleDbCommand command2 = new OleDbCommand(querylan2, conn);
                                command2.CommandType = CommandType.Text;
                                int num = command2.ExecuteNonQuery();
                                if (num == 1)
                                {
                                    MessageBox.Show("密码修改成功");
                                }
                                else
                                {
                                    MessageBox.Show("修改密码失败，请重试");
                                }
                            }
                            else
                            {
                                MessageBox.Show("前后输入的新密码不一致");
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入新密码");
                        }
                    }
                    else
                    {
                        MessageBox.Show("输入的原始密码错误，请重试");
                    }
                    conn.Close();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "密码" && e.Value != null && e.Value.ToString().Length > 0)
            {
                e.Value = new string('*', e.Value.ToString().Length);
            } 

        }

        public bool Login(string user, string password)
        {
              OleDbConnection conn = InitialConnectionDb();
            string querylan = "Select 用户名 ,密码 From UserInfo Where 用户名='" + user + "'" + "And 密码='" + password + "'";
            OleDbCommand command = new OleDbCommand(querylan, conn);
            command.CommandType = CommandType.Text;
            try
            {
                conn.Open();
                OleDbDataReader reader = command.ExecuteReader();
                if (reader.Read())
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = InitialConnectionDb();
            string querylan = "Select 用户名 ,密码 From UserInfo Where 用户名='" + textBox1.Text.Trim() + "'" + "And 密码='" + textBox2.Text.Trim() + "'";
            OleDbCommand command = new OleDbCommand(querylan, conn);
            command.CommandType = CommandType.Text;
            try
            {
                conn.Open();
                OleDbDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    if (reader.GetString(0) == "Admin" && reader.GetString(1) == "Admin")  // 判断是否是管理员身份
                    {
                        button1.Enabled = true;
                        button2.Enabled = true;
                        textBox1.Text = "";
                        textBox2.Text = "";
                        MessageBox.Show("管理身份登录成功,可以进行添加或者删除用户!");
                    }
                    else
                        return;
                }
                else
                {
                    MessageBox.Show("输入的用户名或密码错误");
                }
            }
            catch
            {
                MessageBox.Show("你输入的用户名或密码不正确");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            SelectedNum = e.RowIndex;
            textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        }
    }
}